<?php

namespace STAGE\StageBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use STAGE\UserBundle\Entity\UserRepository;
use STAGE\StageBundle\Form\StagePersonneType;
class StageType extends AbstractType {

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('sujet')
                ->add('dateDebut', 'date', array(
                    'widget' => 'single_text',
                    'input' => 'datetime',
                    'format' => 'dd/MM/yyyy',
                    'attr' => array('class' => 'date'),
                        )
                )
                ->add('dateFin', 'date', array(
                    'widget' => 'single_text',
                    'input' => 'datetime',
                    'format' => 'dd/MM/yyyy',
                    'attr' => array('class' => 'date'),
                        )
                )
                ->add('description')
                ->add('type')
                ->add('etudiants', 'entity', array(
                    'multiple'=>true,
                    'class' => 'STAGE\UserBundle\Entity\User',
                    'query_builder' => function(UserRepository $er) {
                return $er->createQueryBuilder('e')
                        ->where('e.roles LIKE :etudiant')
                        ->setParameter('etudiant', '%ROLE_ETUDIANT%');
                ;
            }
                ))
                ->add('enseignants','collection',array(
                    'type'=> new StagePersonneType(),
                    'allow_add'=>true,
                    'allow_delete'=>true,
                    'by_reference'=>true
                ))
                ->add('encadreur', 'entity', array(
                    'class' => 'STAGE\UserBundle\Entity\User',
                    'query_builder' => function(UserRepository $er) {
                return $er->createQueryBuilder('e')
                        ->where('e.roles LIKE :etudiant')
                        ->setParameter('etudiant', '%ROLE_ENCADREUR%');
                ;
            }
                ))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'STAGE\StageBundle\Entity\Stage'
        ));
    }

    /**
     * @return string
     */
    public function getName() {
        return 'stage_stagebundle_stage';
    }

}
